#include "draw.h"
/*
绘指定坐标点字符
 *str：绘哪个字符,注意是字符串,不是单个字符
*/
void draw_point(HANDLE handle_out,char *str,COORD pos)
{   
    char buf[128];
    if((pos.X>=0)&&(pos.Y>=0)&&(pos.X<=MAX_X)&&(pos.Y<=MAX_Y))
    {
        framebuffer[pos.X][pos.Y]=*str;//存储到显存数组，方便后期扩展图像算法处理源
        #ifdef DIRECT_SHOW //开启直接屏幕绘制，提高绘制效率
        WriteConsoleOutputCharacter(handle_out,str, 1, pos, buf);   //输出字符串
        #endif

    }
}

/*
 画线,由于时间问题，算法实现的效果不怎么好，且用到了浮点，移植到不支持浮点的单片机上效率不是很高，不过x86不要太介意。
 pos_start：画线的起始坐标
 pos_stop：画线的终止坐标
 *s：选用哪个字符绘
*/


void draw_line(HANDLE handle_out,COORD pos_start,COORD pos_stop,char *s)

{
int x0,y0,x1, y1;
   int x;
   int swap_tmp;
   float dx, dy, y, k;
    COORD pos_pen={0,0};
    x0=pos_start.X;
    y0=pos_start.Y;
    x1=pos_stop.X;
    y1=pos_stop.Y;

    if(x0>x1)
    {
         swap_tmp=x0;
         x0=x1;
         x1=swap_tmp;
         swap_tmp=y0;
         y0=y1;
         y1=swap_tmp;
    }
    if(x1==x0)
    {

        if(y0>y1)
        {
            swap_tmp=y0;
            y0=y1;
            y1=swap_tmp;
        }
        pos_pen.X=x1;
        for(pos_pen.Y=y0;pos_pen.Y<y1;pos_pen.Y++)
        {
            draw_point( handle_out,s,pos_pen);
        }
        return 1;
    }

   dx = x1-x0;
   dy=y1-y0;
   k=dy/dx;
   y=y0;

   for (x=x0;x< x1;x++)
   {

       pos_pen.X=x;
       pos_pen.Y=(int)(y+0.5);
       draw_point( handle_out,s,pos_pen);
       y=y+k;

   }

}

/*
   画个矩形
   ori_pos：矩形的起始坐标点
   fill_flag：为1则矩形内部也填充，为0则只绘框架
*/
void draw_rect(HANDLE handle_out,COORD ori_pos,int width,int heigth,char *s,int fill_flag)
{
    COORD a,b,c;
    COORD pos_pen={0,0};
    a.X=ori_pos.X+width;
    a.Y=ori_pos.Y;
    b.X=ori_pos.X;
    b.Y=ori_pos.Y+heigth;
    c.X=ori_pos.X+width;
    c.Y=ori_pos.Y+heigth;

    draw_line(handle_out,ori_pos,a,s);
    draw_line(handle_out,ori_pos,b,s);
    draw_line(handle_out,a,c,s);
    draw_line(handle_out,b,c,s);
    if(fill_flag==1)
    {
        for(pos_pen.X=ori_pos.X;pos_pen.X<=a.X;pos_pen.X++)
             for(pos_pen.Y=ori_pos.Y;pos_pen.Y<=b.Y;pos_pen.Y++)
                {
                    draw_point( handle_out,s,pos_pen);
                }
    }
}

/*
画圆：效果很差
pos_ori：圆心坐标
r：半径
fill_flag：为1画实心圆，为0画空心圆
inaccuracy：调整画圆的精度

*/

void draw_circle ( HANDLE handle_out, COORD pos_ori,int r,char *s,int fill_flag ,int inaccuracy)

{

 int x, y;
 COORD pos_pen;
 for(x=pos_ori.X-r;x<pos_ori.X+r;x++)
     for(y=pos_ori.Y-r;y<pos_ori.Y+r;y++)
     {
         if(fill_flag==1)
         {
           if((x-pos_ori.X)*(x-pos_ori.X)+(y-pos_ori.Y)*(y-pos_ori.Y)<=r*r)
           {
               pos_pen.X=x;
               pos_pen.Y=y;
               draw_point( handle_out,s,pos_pen);
           }
         }
         else
         {
             if(((x-pos_ori.X)*(x-pos_ori.X)+(y-pos_ori.Y)*(y-pos_ori.Y)<(r*r+inaccuracy))&&((x-pos_ori.X)*(x-pos_ori.X)+(y-pos_ori.Y)*(y-pos_ori.Y)>(r*r-inaccuracy)))
             {
                 pos_pen.X=x;
                 pos_pen.Y=y;
                 draw_point( handle_out,s,pos_pen);
             }
         }
      }

}